package com.gdut.simple.view.activity.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.gdut.simple.R;
import com.gdut.simple.common.LogUtils;
import com.gdut.simple.contract.CommentContract;
import com.gdut.simple.data.entity.Comment;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.presenter.user.CommentPresenter;
import com.gdut.simple.view.activity.explore.PostDetailActivity;
import com.gdut.simple.view.adapter.MyCommentAdapter;
import com.gdut.simple.view.adapter.base.AbsRecyclerAdapter;
import com.gdut.simple.view.basic.AbsBasicActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobUser;

public class CommentActivity extends AbsBasicActivity implements CommentContract.View {

    @BindView(R.id.id_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.id_item_list_view)
    RecyclerView mCommentListView;
    @BindView(R.id.id_swipe_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    private MyCommentAdapter mCommentAdapter;
    private List<Comment> mCommentList;
    private CommentContract.Presenter mCommentPresenter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_comment);
        ButterKnife.bind(this);
        new CommentPresenter(this);
        initToolbar();
        initRefreshLayout();
        initRecyclerView();
    }

    private void initToolbar() {
        mToolbar.setTitle("评论");
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
        }
    }

    private void initRefreshLayout() {
        mSwipeRefreshLayout.setColorSchemeColors(ContextCompat.getColor(this, R.color.colorPrimaryDark),
                ContextCompat.getColor(this, R.color.colorPrimary),
                ContextCompat.getColor(this, R.color.colorPrimaryDark));
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mCommentPresenter.refresh();
            }
        });
    }

    private void initRecyclerView() {
        mCommentListView.setLayoutManager(new LinearLayoutManager(this));
        mCommentListView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mCommentListView.setItemAnimator(new DefaultItemAnimator());
        mCommentList = new ArrayList<>();
        mCommentAdapter = new MyCommentAdapter(this, mCommentList);
        mCommentListView.setAdapter(mCommentAdapter);
        mCommentAdapter.setOnItemClickListener(new AbsRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                LogUtils.i("intent cpmment:" + mCommentList.get(position));
                LogUtils.i("intent post:" + mCommentList.get(position).getPost());
                Intent intent = new Intent(CommentActivity.this, PostDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("id", mCommentList.get(position).getPost().getObjectId());
                bundle.putParcelable("post", mCommentList.get(position).getPost());
                bundle.putString("time", mCommentList.get(position).getPost().getCreatedAt());
                bundle.putString("profile", mCommentList.get(position).getPost().getAuthor().getProfile().getUrl());
                bundle.putString("name", mCommentList.get(position).getPost().getAuthor().getNickName());
                intent.putExtras(bundle);
                startActivity(intent);
            }

            @Override
            public boolean onLongClick(View view, int position) {
                return false;
            }
        });
    }

    @Override
    protected void loadData() {
        mCommentPresenter.refresh();
    }

    @Override
    public List<Comment> getItemList() {
        return mCommentList;
    }

    @Override
    public void refreshList() {
        if (mCommentAdapter != null) {
            mCommentAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showRefreshProgress() {
        if (mSwipeRefreshLayout != null) {
            mSwipeRefreshLayout.setRefreshing(true);
        }
    }

    @Override
    public void hideRefreshProgress() {
        if (mSwipeRefreshLayout != null && mSwipeRefreshLayout.isRefreshing()) {
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }

    @Override
    public User getUser() {
        if (getIntent().getExtras() != null) {
            User user = getIntent().getExtras().getParcelable("user");
            String objectId = getIntent().getExtras().getString("objectId");
            if (user != null && objectId != null && !objectId.isEmpty()) {
                user.setObjectId(objectId);
                return user;
            }
        }
        return BmobUser.getCurrentUser(User.class);
    }

    @Override
    public void setPresenter(@NonNull CommentContract.Presenter presenter) {
        mCommentPresenter = presenter;
    }
}
